package com.wan.zuochengyun_v2.stack_queue;

import java.util.LinkedList;

/**
 * 2.stack和queue的获取队头或栈头都用peek方法，都不会抛出异常
 * 1.poll弹出队列/pop出栈都会返回元素，但是pop会抛出NoSuchElementException【pop调用了removeFirst，poll没有】
 *
 * */
public class MyStack<T> {
    private LinkedList<T> stack;

    public MyStack() {
        this.stack = new LinkedList();
    }

    /**
     * 入栈，list头部入栈
     */
    public void push(T val) {
        stack.push(val);
    }

    /**
     * throw NoSuchElementException
     */
    public T pop() {
        return stack.pop();
    }

    /**
     * null
     */
    public T top() {
        return stack.peek();
    }

    public int size() {
        return stack.size();
    }

    public static void main(String[] args) {
        System.out.println("\n************************Deque**********************");
        MyStack<Integer> stack = new MyStack<>();
        stack.push(3);
        stack.push(2);
        stack.push(1);
        System.out.println(stack.top());
        for (int i = stack.size(); i > 0; i--) {
            System.out.print(stack.pop() + ",");
        }
    }
}
